<?php
/**
 * 邮件部分
 * 
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-03
 * @author monkee
 */

class mailerApp extends app
{
	public function index(){
		$data = & $this->getData('get');

		$pagesize = $data['pagesize']==''?10:intval($data['pagesize']);
		$pagenum = $data['pagenum']==''?1:intval($data['pagenum']);
		
		$mailermod = & $this->getModel('mailer');
		$mailers = $mailermod->get_mailers_by_user_id($this->user_id);		
		if(count($mailers)==0){
			go_to('/setting/mail');
		}
		$r = $mailermod->receivebox($this->user_id,$totalcount,$totalpage,$pagesize,$pagenum);
		if($r==-1){
			return array('code'=>1, 'msg' => '邮箱不存在！');
		}else{
			return array('code'=>2,'mails'=>$r,'totalcount'=>$totalcount,'totalpage'=>$totalpage,'pagenum'=>$pagenum,'pagesize'=>$pagesize);
		}
	}

	/**************************************************************************/
	/*
	/*收件箱部分
	/*
	/**************************************************************************/
	public function receive(){
		
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->receive($this->user_id);
		
		switch($r){
			case -1	: echo -1;exit;		//邮箱不存在
			case 1	: echo 2;exit;		//收取成功
			default	: echo -2;exit;		//收取失败
		}
	}
	
	public function lastmailcount(){
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->last_mail_count($this->user_id);
		if($r>=0){
			echo $r;
			exit;
		}elseif($r==-1){
			echo -1;		//邮箱不存在
			exit;
		}elseif($r==-2){
			echo -2;		//邮箱配置信息有误，链接失败！
			exit;
		}else{
			echo -3;		//链接失败
			exit;
		}
	}
	
	public function receivebynumber(){
		$data = $this->getData('post');
		if(empty($data)){
			return array('code'=>0,'msg' => '操作失败！');
		}
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->receive_by_number($this->user_id,intval($data['start']),intval($data['start']));
		switch($r){
			case -1	: return array('code'=>1, 'msg' => '邮箱不存在！');
			case 1	: return array('code'=>2, 'msg' => '收取成功！');
			default	: return array('code'=>1, 'msg' => '收取失败！');
		}
	}

	public function receivebox(){
		$data = & $this->getData('post');

		$pagesize = $data['pagesize']==''?10:intval($data['pagesize']);
		$pagenum = $data['pagenum']==''?1:intval($data['pagenum']);
		
		$mailermod = & $this->getModel('mailer');		
		$r = $mailermod->receivebox($this->user_id,$totalcount,$totalpage,$pagesize,$pagenum);
		if($r==-1){
			return array('code'=>1, 'msg' => '邮箱不存在！');
		}else{
			$mailboxs = $mailermod->get_mailers_by_user_id($this->user_id);
			return array('code'=>2,'mails'=>$r,'mailboxs'=>$mailboxs,'totalcount'=>$totalcount,'totalpage'=>$totalpage,'pagenum'=>$pagenum,'pagesize'=>$pagesize);
		}
	}

	public function viewreceivemail(){
		$data = $this->getData('get');
		if(empty($data)){
			return array('code'=>0);
		}
		$mailid = intval($data['mailid']);
		
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->viewreceivemail($this->user_id,$mailid);
		if($r==-1){
			return array('code'=>1, 'msg' => '邮箱不存在！');
		}else{
			return array('code'=>2 , 'mail'=>$r);
		}
	}
	
	public function receivemailcontent(){
		$data = $this->getData('get');
		if(empty($data)){
			return array('code'=>0);
		}
		$mailboxid = intval($_COOKIE['mailer_mailboxid']);
		$mailid = intval($data['mailid']);
		
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->viewreceivemail($this->user_id,$mailboxid,$mailid);
		if($r==-1){
			return array('code'=>1, 'msg' => '邮箱不存在！');
		}else{
			return array('code'=>2 , 'mail'=>$r);
		}
	}
	
	public function clearreceivebox(){
		$data = $this->getData('post');
		if(empty($data)){
			return array('code'=>0);
		}
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->clearsendbox($this->user_id,intval($data['mailboxid']));
		switch($r){
			case -1	: return array('code'=>1, 'msg' => '邮箱不存在！');
			case 1	: return array('code'=>2, 'msg'=> '收件箱清空成功！');
			default	: return array('code'=>1, 'msg' => '操作失败！');
		}
	}

	public function deletereceivemail(){
		$data = $this->getData('get');
		if(empty($data)){
			return array('code'=>0);
		}
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->deletereceivemail($this->user_id,intval($data['mailid']));
		switch($r){
			case -1	: go_to('/mailer/index', '邮箱不存在！');
			case 1	: go_to('/mailer/index', '邮件删除成功！');
			default	: go_to('/mailer/index', '操作失败！');
		}
	}
	/**************************************************************************/
	/*
	/*发件箱部分
	/*
	/**************************************************************************/

	public function attachment(){
		$filename = $this->getData('get/qqfile');
		if(empty($filename)){
			Cookie::set('mailer_send_file_path','');
			return array();
		}else{
			$file = file_get_contents("php://input");
			if(empty($file)){
				return array('error' => '文件上传失败');
			}
			$file_size = strlen($file);
			$mailermod = & $this->getModel('mailer');
			$local_name = $mailermod->save_file($file, $filename);
			$local_name=MAILER_ATTACHMENT_PAHT."/tmp/".$local_name;
			$mailer_send_file_path = Cookie::get('mailer_send_file_path');
			$mailer_send_file_path.="|".$local_name;
			Cookie::set('mailer_send_file_path',$mailer_send_file_path);
			return array('success' => true);
		}
	}
	public function send(){
		$replyto = $this->getData('get/to');
		if(!empty($replyto)){
			return array("code"=>0,'replyto'=>$replyto);
		}else{
			return array("code"=>0);
		}	
	}
	
	public function exesend(){
		$data = $this->getData('post');
		if(empty($data)){
			return array("code"=>0);
		}
		$mailermod = & $this->getModel('mailer');
		$attachment = array();
		//附件上传,保存到临时地址$attachment中
		$mailer_send_file_path = Cookie::get('mailer_send_file_path');
		$mailer_send_file_path = substr($mailer_send_file_path,1);//去除第一个|
		$attachment = explode("|",$mailer_send_file_path);
		$r = $mailermod->send($this->user_id,addslashes($data['to']),addslashes($data['subject']),addslashes($data['body']),$attachment);

		//清空附件地址
		Cookie::set('mailer_send_file_path','');
		switch($r){
			case -1	: go_to('/mailer/send', '邮箱不存在！');
			case 1	: go_to('/mailer/sendbox', '发送成功！');
			default	: go_to('/mailer/send', '发送失败！');
		}
	}

	public function sendbox(){
		$data = $this->getData('get');
		$pagesize = $data['pagesize']==''?10:intval($data['pagesize']);
		$pagenum = $data['pagenum']==''?1:intval($data['pagenum']);

		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->sendbox($this->user_id,$totalcount,$totalpage,$pagesize,$pagenum);
		if($r==-1){
			return array('code'=>1, 'msg' => '邮箱不存在！');
		}else{
			
			return array('code'=>2,'mails'=>$r,'totalcount'=>$totalcount,'totalpage'=>$totalpage,'pagenum'=>$pagenum,'pagesize'=>$pagesize);
		}
	}
	
	public function viewsendmail(){
		$data = $this->getData('get');
		$mailid = intval($data['mailid']);
		
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->viewsendmail($this->user_id,$mailid);
		if($r==-1){
			return array('code'=>1, 'msg' => '邮箱不存在！');
		}else{
			return array('code'=>2 , 'mail'=>$r);
		}
	}
	
	public function sendmailcontent(){
		$data = $this->getData('get');
		if(empty($data)){
			return array('code'=>0);
		}
		$mailboxid = intval($_COOKIE['mailer_mailboxid']);
		$mailid = intval($data['mailid']);
		
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->viewsendmail($this->user_id,$mailboxid,$mailid);
		if($r==-1){
			return array('code'=>1, 'msg' => '邮箱不存在！');
		}else{
			return array('code'=>2 , 'mail'=>$r);
		}
	}
	
	public function clearsendbox(){
		$data = $this->getData('post');
		if(empty($data)){
			return array('code'=>0);
		}
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->clearsendbox($this->user_id,intval($data['mailboxid']));
		switch($r){
			case -1	: return array('code'=>1, 'msg' => '邮箱不存在！');
			case 1	: return array('code'=>2, 'msg'=> '发件箱清空成功！');
			default	: return array('code'=>1, 'msg' => '操作失败！');
		}
	}
	
	public function deletesendmail(){
		$data = $this->getData('get');
		if(empty($data)){
			return array('code'=>0);
		}
		$mailermod = & $this->getModel('mailer');
		$r = $mailermod->deletesendmail($this->user_id,intval($data['mailid']));
		switch($r){
			case -1	: go_to('/mailer/sendbox', '邮箱不存在！');
			case 1	: go_to('/mailer/sendbox', '邮件删除成功！');
			default	: go_to('/mailer/sendbox', '操作失败！');
		}
	}
}
?>